﻿using System.Collections.ObjectModel;
using System.IO;
using HalconDotNet;
using MachineVision.Core;
using MachineVision.Core.OCR;
using MachineVision.Core.TemplateMatch.Shared;
using MachineVision.Shared.Controls;
using Microsoft.Win32;
using Prism.Commands;

namespace MachineVision.Ocr.ViewModels
{
    public class QrCodeViewModel : NavigationViewModel
    {
        public QrCodeViewModel(QrCodeService qrCodeService)
        {
            QrCodeService = qrCodeService;
            DrawInfos = new ObservableCollection<DrawingObjectInfo>();
            MatchResult = new OcrMatchResult();
        }

        public QrCodeService QrCodeService { get; }

        private void ExecuteLoadImageCommand()
        {
            Image?.Dispose();
            DrawInfos?.Clear();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == true)
            {
                if (File.Exists(ofd.FileName))
                {
                    try
                    {
                        HObject tmpimage;
                        HOperatorSet.GenEmptyObj(out tmpimage);
                        HOperatorSet.ReadImage(out tmpimage, ofd.FileName);
                        Image = tmpimage;
                        //Result.Message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:图像加载完成";
                    }
                    catch (Exception)
                    {
                        MatchResult.Message =
                            $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:加载图像失败,该图像不受支持";
                    }
                }
            }
        }

        private void ExecuteRunCommand()
        {
            try
            {
                if (Image == null)
                    throw new Exception("图像源不可为空");
                var res = QrCodeService.Run(Image);
                if (!res.IsSuccess)
                    throw new Exception(res.Message);
                MatchResult.Message = res.Message;
            }
            catch (Exception ex)
            {
                MatchResult.Message =
                    $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}:运行出错,{ex.Message}";
            }
        }

        private void ExecuteSetRangedCommand()
        {
            var hobj = DrawInfos?.FirstOrDefault();
            if (hobj != null)
            {
                if (hobj.ShapeType == ShapeType.Rectange)
                {
                    QrCodeService.Roi = new ROIParameter();
                    QrCodeService.Roi.Row1 = hobj.HTuples[0];
                    QrCodeService.Roi.Column1 = hobj.HTuples[1];
                    QrCodeService.Roi.Row2 = hobj.HTuples[2];
                    QrCodeService.Roi.Column2 = hobj.HTuples[3];
                    MatchResult.Message =
                        $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}: Roi区域配置完成";
                    DrawInfos.Remove(hobj);
                    return;
                }
            }
            QrCodeService.Roi = null;
            MatchResult.Message = $"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}: 请绘制Roi区域后点击按钮";
        }

        #region Properties

        private HObject image;

        /// <summary>
        /// 加载的图像
        /// </summary>
        public HObject Image
        {
            get { return image; }
            set
            {
                image = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<DrawingObjectInfo> drawInfos;

        /// <summary>
        /// 绘制的图像集合
        /// </summary>
        public ObservableCollection<DrawingObjectInfo> DrawInfos
        {
            get { return drawInfos; }
            set
            {
                drawInfos = value;
                RaisePropertyChanged();
            }
        }

        private OcrMatchResult matchResult;
        public OcrMatchResult MatchResult
        {
            get { return matchResult; }
            set
            {
                matchResult = value;
                RaisePropertyChanged();
            }
        }
        #endregion
        #region Command
        private DelegateCommand loadImageCommand;
        public DelegateCommand LoadImageCommand =>
            loadImageCommand ?? (loadImageCommand = new DelegateCommand(ExecuteLoadImageCommand));
        private DelegateCommand runCommand;
        public DelegateCommand RunCommand =>
            runCommand ?? (runCommand = new DelegateCommand(ExecuteRunCommand));
        private DelegateCommand setRangedCommand;
        public DelegateCommand SetRangedCommand =>
            setRangedCommand ?? (setRangedCommand = new DelegateCommand(ExecuteSetRangedCommand));
        #endregion
    }
}
